package com.gcxy.jk.shixunyi.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.gcxy.jk.shixunyi.dto.WxUserDto;
import com.gcxy.jk.shixunyi.dto.WxUserResponseDto;
import com.gcxy.jk.shixunyi.entity.User;
import com.gcxy.jk.shixunyi.service.UserService;
import com.gcxy.jk.shixunyi.utils.WeChatUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Random;
import java.util.Scanner;

/**
 * 用于微信登录
 */
@Slf4j
@RestController
public class WxLoginController {

    @Value("${wxMini.appId}")
    private String appId;

    @Value("${wxMini.secret}")
    private String secret;

    private String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";

    @Autowired
    private UserService userService;

    @ApiOperation(value = "微信登录接口", notes = "微信登录接口")
    @PostMapping("/wxLogin")
    public WxUserResponseDto wxLogin(@RequestBody WxUserDto userDto) {
        String url = loginUrl.replace("APPID", appId)
                .replace("SECRET", secret)
                .replace("JSCODE", userDto.getCode());

        // 去微信小程序官网获取openid
        String response = WeChatUtil.httpRequest(url, "GET", "");
        log.info(response);
        JSONObject parseObj = JSONUtil.parseObj(response);
        log.info(parseObj.toString());
        String openid = parseObj.getStr("openid");

        WxUserResponseDto responseDto = userService.bind(userDto.getNickName(), userDto.getAvatarUrl(), userDto.getGender(), openid);

        return responseDto;
    }

    @ApiOperation(notes = "微信用户详细信息", value = "微信用户信息")
    @GetMapping(value = "/getUserMessageByOpenid")
    public WxUserResponseDto getUserMessageByOpenid(String openid){

        return userService.getUserMessageByOpenid(openid);
    }

    @ApiOperation(notes = "绑定手机号", value = "绑定手机号")
    @PostMapping("/insertPhone")
    public String insertPhone(String phone){

//        int max=10000;
//        int min=1000;
//        Random random = new Random();//
//        String error = "验证码错误";
//        int s =  (random.nextInt(max - min) + min + 1);
//        System.out.println(s);
//        if (getNums() == s) {
        String pass = "绑定成功";
        userService.insertPhone(phone);
        return pass;
    }
    @PostMapping("/phoneNames")
    public Integer getNums(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入验证码");
        int i = scanner.nextInt();
        return i;
    }
    @ApiOperation(notes = "城市绑定", value = "城市绑定")
    @PostMapping("/insertProv")
    public void insertProv(String prov){
        userService.insertProv(prov);
    }

    @ApiOperation(notes = "编辑资料", value = "编辑资料")
    @PostMapping("/editMessage/{openid}")
    public void editMessage(@PathVariable("openid") String openid,String prov,String telephone,String nikeName){
        userService.editMessage(openid,prov,telephone,nikeName);
    }

    @ApiOperation(notes = "用户余额减少", value = "用户余额减少")
    @PostMapping("/subMoney/{openid}")
    public  void subMoney(BigDecimal money,@PathVariable("openid") String openid){
        userService.subMoney(money,openid);
    }

    @ApiOperation(notes = "用户余额增加", value = "用户余额增加")
    @PostMapping("/addMoney/{openid}")
    public  void addMoney(BigDecimal money,@PathVariable("openid") String openid){
        userService.addMoney(money,openid);
    }
}
